package com.passive.lock.controller.auth;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passive.lock.controller.BaseController;
import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TAuthVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.auth.AuthService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("au")
public class AuthController extends BaseController {

	@Autowired
	private AuthService authService;

	/**
	 * 任务信息管理
	 *
	 */

	@RequestMapping(value = "/authlist")
	@ResponseBody
	public Result authList(@RequestParam(required = false) String groupid,@RequestParam long beginTime, @RequestParam long endTime,
			@RequestParam(required = false) String authname, @RequestParam(required = false) String username,
			@RequestParam(required = false) String employeename, @RequestParam(required = false) String authtype,
			@RequestParam Integer page, @RequestParam Integer limit, HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		if(groupid == null||"".equals(groupid)){
			groupid = tUserVo.getGroupid();
		}
		try {
			int offset = PageUtil.getOffset(page, limit);
			List<TAuthVo> list = authService.authList(groupid,beginTime,endTime,authname, username,employeename,authtype,offset, limit);
			int count = authService.authListCount(tUserVo.getGroupid(),beginTime,endTime,authname, username,employeename,authtype);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addauth")
	@ResponseBody
	public Result addAuth(@RequestBody TAuthVo tAuthVo) {
		try {
			Result result = authService.addAuth(tAuthVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updateauth")
	@ResponseBody
	public Result updateAuth(@RequestBody TAuthVo tAuthVo) {
		try {
			Result result = authService.updateAuth(tAuthVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteauth")
	@ResponseBody
	public Result deleteAuth(@RequestBody TAuthVo tAuthVo) {
		try {
			Result result = authService.deleteAuth(tAuthVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteauthlist")
	@ResponseBody
	public Result deleteAuthList(@RequestBody List<Integer> tAuthids) {
		try {
			Result result = authService.deleteAuthList(tAuthids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

}
